﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusLib
{
    public partial class Shape_Point
    {
        public LatLong Position { get { return new LatLong(shape_pt_lat, shape_pt_lon); } }

        static Shape last_shape=null;
        static int transactionPendingCount=0;
        static public Shape_Point ImportCSV(string line)
        {
            Shape_Point point = null;
            Shape shape = null;
            string[] fields = line.Split(',');

            if (fields[0] != "shape_id")
            {
                int shape_id = int.Parse(fields[0]);
                if (last_shape != null && last_shape.shape_id == shape_id)
                {
                    shape = last_shape;
                }
                else
                {
                    shape = Shape.Get(shape_id);
                    if (shape == null)
                    {
                        shape = new Shape();
                        shape.shape_id = shape_id;
                        BusLibDataContext.db.Shapes.InsertOnSubmit(shape);
                    }
                    last_shape = shape;
                }
                point = new Shape_Point();

                point.Shape = shape;
                point.shape_pt_lat = double.Parse(fields[1]);
                point.shape_pt_lon = double.Parse(fields[2]);
                point.shape_pt_sequence = int.Parse(fields[3]);
                if (fields[4] != "")
                {
                    point.shape_dist_travelled = int.Parse(fields[4]);
                }

                BusLibDataContext.db.Shape_Points.InsertOnSubmit(point);

                if (++transactionPendingCount > 10000)
                {
                    BusLibDataContext.db.SubmitChanges();
                    transactionPendingCount = 0;
                }
            }
            return point;
        }
    }
}
